import useStore1 from "@/store/store1"
//判断角色
const checkRoles = (menuRoles: any) => {
    if (menuRoles === undefined)
        return false
    const pinaRoles = useStore1().user.roles;
    let flag = false
    if (typeof (menuRoles) === 'string') {
        flag = pinaRoles.includes(menuRoles)
    } else if (menuRoles instanceof Array) {
        for (const role of menuRoles) {
            if (pinaRoles.includes(role)) {
                flag = true
                break
            }
        }
    } else {
        throw new Error('请绑定角色或权限值')
    }

    return flag
}
//判断权限
const checkPermissions = (buttonPermission: any) => {
    if (buttonPermission === undefined)
        return false
    const pinaPermissions = useStore1().user.permission;
    let flag = false
    if (typeof (buttonPermission) === 'string') {
        flag = pinaPermissions.includes(buttonPermission)
    } else if (buttonPermission instanceof Array) {
        for (const permission of buttonPermission) {
            if (pinaPermissions.includes(permission)) {
                flag = true
                break
            }
        }
    } else {
        throw new Error('请绑定角色或权限值')
    }

    return flag
}

export default {
    name: "auth",
    mounted: (el: any, binding: any) => {
        
         const type = binding.arg
         if(type === 'role'){
            if (!checkRoles(binding.value)) {
                el.remove()
            }
         }
         if(type === 'permission'){
            if (!checkPermissions(binding.value)) {
                el.remove()
            }
         }
       
    },

}